package cn.jh.clearing.business.impl;

import java.math.BigDecimal;
import java.net.URI;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import cn.jh.clearing.business.ProfitRecordBusiness;
import cn.jh.clearing.pojo.DistributionRecord;
import cn.jh.clearing.pojo.ProfitRecord;
import cn.jh.clearing.repository.DistributionRecordRepository;
import cn.jh.clearing.repository.PaymentOrderRepository;
import cn.jh.clearing.repository.ProfitRecordRepository;
import cn.jh.clearing.service.ProfitService;
import cn.jh.clearing.util.Util;
import cn.jh.common.utils.CommonConstants;
import net.sf.json.JSONObject;

@Service
public class ProfitRecordBusinessImpl implements ProfitRecordBusiness{
	
	private static final Logger LOG = LoggerFactory.getLogger(ProfitService.class);
	@Autowired
    private ProfitRecordRepository profitRecordRepository;
	
	@Autowired
    private DistributionRecordRepository disRecordRepository;
	
	@Autowired
    private PaymentOrderRepository paymentOrderRepository;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	private EntityManager em;
	
	
	@Autowired
	Util util;

	@Override
	public Page<ProfitRecord> findProfitByUserid(long userid, Pageable pageAble) {
		return profitRecordRepository.findProfitByUserid(userid, pageAble);
	}

	@Transactional
	@Override
	public ProfitRecord merge(ProfitRecord profitRecord) {
		ProfitRecord  record =  profitRecordRepository.save(profitRecord);
		em.flush();
		em.clear();
		return record;
	}

	@Override
	public Page<ProfitRecord> findProfitByUserid(String userid,String grade , Date startTime,
			Date endTime, Pageable pageAble) {
		long brandid=0l;
		if(grade==null||grade.equals("")){
			
			if(userid != null && !userid.equalsIgnoreCase("")){
				if(startTime != null){
					
					if(endTime != null){
						
						return profitRecordRepository.findAllProfitRecord(Long.parseLong(userid), startTime, endTime, pageAble);
						
					}else{
						return profitRecordRepository.findAllProfitRecord(Long.parseLong(userid), startTime, pageAble);
					}
					
				}else{
					
					
					return profitRecordRepository.findProfitByUserid(Long.parseLong(userid), pageAble);
					
				}
				
			}else{
				
				
				return profitRecordRepository.findAll(pageAble);
				
			}
			
		}else{
			Long[] uids=this.findUserByGrade(grade,brandid);
			if(userid != null && !userid.equalsIgnoreCase("")){
				long useid=Long.parseLong(userid);
				String start="n";
				for(long uid:uids){
					if(uid==useid){
						start="y";
					}
				}
				if(start.equals("n")){
					userid="0";
				}
				
				if(startTime != null){
					
					if(endTime != null){
						
						return profitRecordRepository.findAllProfitRecord(Long.parseLong(userid), startTime, endTime, pageAble);
						
					}else{
						return profitRecordRepository.findAllProfitRecord(Long.parseLong(userid), startTime, pageAble);
					}
					
				}else{
					
					
					return profitRecordRepository.findProfitByUserid(Long.parseLong(userid), pageAble);
					
				}
				
			}else{
				
				
				return profitRecordRepository.findProfitByBrandId(uids,pageAble);
				
			}
			
			
			
		}	
		
	
	}
	
	@Override
	public Page<ProfitRecord> findProfitByBrandId(long brandid,String userid,String grade , Date startTime,
			Date endTime, Pageable pageAble) {
		
		if(grade==null||grade.equals("")){
			Long[] uids=this.findUserByBrandId(brandid);
			if(userid != null && !userid.equalsIgnoreCase("")){
				long useid=Long.parseLong(userid);
				String start="n";
				for(long uid:uids){
					if(uid==useid){
						start="y";
					}
				}
				if(start.equals("n")){
					userid="0";
				}
				if(startTime != null){
					if(endTime != null){
						return profitRecordRepository.findAllProfitRecord(Long.parseLong(userid), startTime, endTime, pageAble);
					}else{
						return profitRecordRepository.findAllProfitRecord(Long.parseLong(userid), startTime, pageAble);
					}
				}else{
					return profitRecordRepository.findProfitByUserid(Long.parseLong(userid), pageAble);
				}
			}else{
				return profitRecordRepository.findProfitByBrandId(uids,pageAble);
			}
		}else{
			Long[] uids=this.findUserByGrade(grade,brandid);
			if(userid != null && !userid.equalsIgnoreCase("")){
				long useid=Long.parseLong(userid);
				String start="n";
				for(long uid:uids){
					if(uid==useid){
						start="y";
					}
				}
				if(start.equals("n")){
					userid="0";
				}
				if(startTime != null){
					if(endTime != null){
						return profitRecordRepository.findAllProfitRecord(Long.parseLong(userid), startTime, endTime, pageAble);
					}else{
						return profitRecordRepository.findAllProfitRecord(Long.parseLong(userid), startTime, pageAble);
					}
				}else{
					return profitRecordRepository.findProfitByUserid(Long.parseLong(userid), pageAble);
				}
			}else{
				return profitRecordRepository.findProfitByBrandId(uids,pageAble);
			}
			
		}
		
	
	}
	public Long[]  findUserByGrade(String  grade,long brandid) {
		URI uri = util.getServiceUrl("user", "error url request!");
		String url = uri.toString() + "/v1.0/user/query/grade";
		/**根据的用户手机号码查询用户的基本信息*/
		MultiValueMap<String, String> requestEntity  = new LinkedMultiValueMap<String, String>();
		requestEntity.add("grade", grade);
		requestEntity.add("brand_id", brandid+"");
		RestTemplate restTemplate=new RestTemplate();
		String result = restTemplate.postForObject(url, requestEntity, String.class);
		LOG.info("RESULT================"+result);
		JSONObject jsonObject =  JSONObject.fromObject(result);
		Long[] str2 = new Long[]{0l};
		JSONObject object =  jsonObject.getJSONObject("result");
		String ids=object.getString("uids");
		if(ids.length()>0){
			ids = ids.substring(0, ids.length()-1);
			String[] str1 = ids.split(",");
	        str2 = new Long[str1.length];
	        for (int j = 0; j < str1.length; j++) {
	            str2[j] = Long.valueOf(str1[j]);
	        }
		}
		return str2;
	}
	public Long[]  findUserByBrandId(long brandid) {
		URI uri = util.getServiceUrl("user", "error url request!");
		String url = uri.toString() + "/v1.0/user/query/brandid";
		/**根据brand_id查询用户的基本信息*/
		MultiValueMap<String, String> requestEntity  = new LinkedMultiValueMap<String, String>();
		requestEntity.add("brand_id", brandid+"");
		RestTemplate restTemplate=new RestTemplate();
		String result = restTemplate.postForObject(url, requestEntity, String.class);
		LOG.info("RESULT================"+result);
		JSONObject jsonObject =  JSONObject.fromObject(result);
		Long[] str2 = new Long[]{0l};
		JSONObject object =  jsonObject.getJSONObject("result");
		String ids=object.getString("uids");
		if(ids.length()>0){
			ids = ids.substring(0, ids.length()-1);
			String[] str1 = ids.split(",");
	        str2 = new Long[str1.length];
	        for (int j = 0; j < str1.length; j++) {
	            str2[j] = Long.valueOf(str1[j]);
	        }
		}
		return str2;
	}
	
	@Override
	public ProfitRecord queryProfitRecordByordercode(String ordercode, String type) {
		return profitRecordRepository.findProfitRecordByordercodeAndtype(ordercode, type);
	}
	@Override
	public  BigDecimal findsumProfitRecord(long acquserid){
		
		
		return profitRecordRepository.findsumProfitRecord(acquserid);
	}
	/**-----------------------------返佣-------------------------**/
	@Transactional
	@Override
	public DistributionRecord merge(DistributionRecord profitRecord) {
		DistributionRecord record =  disRecordRepository.save(profitRecord);
		em.flush();
		return record;
	}

	@Override
	public Page<DistributionRecord> findDistributionRecordByUserid(
			long userid, Date startTime, Date endTime, Pageable pageAble) {
			if(startTime!=null){
				if(endTime!=null){
					return disRecordRepository.findDistributionRecordByUserId(userid, startTime, endTime, pageAble);
				}else{
					return disRecordRepository.findDistributionRecordByUserIdstart(userid, startTime, pageAble);
				}
				
			}else if(endTime!=null){
				return disRecordRepository.findDistributionRecordByUserIdend(userid, startTime, pageAble);
				
			}else{
				return disRecordRepository.findDistributionRecordByUserid(userid, pageAble);
			}
		
	}

	@Override
	public Page<DistributionRecord> findDistributionRecordByPlatform(long brandid, String userid, String order_code, Date startTime,
			Date endTime, Pageable pageAble) {
		if(brandid==-1){
			if(userid!=null&&!userid.equals("")){
				if(order_code!=null&&!order_code.equals("")){
					if(startTime!=null){
						if(endTime!=null){
							return disRecordRepository.findDistributionRecordByUseridOrdercodestartend(Long.parseLong(userid), order_code, startTime, endTime, pageAble);
							
						}else{
							return disRecordRepository.findDistributionRecordByUseridOrdercodestart(Long.parseLong(userid), order_code, startTime, pageAble);
						}
						
					}else if(endTime!=null){
						
						return disRecordRepository.findDistributionRecordByUseridOrdercodeend(Long.parseLong(userid), order_code, endTime, pageAble);
					}else{
						
						return disRecordRepository.findDistributionRecordByUseridOrdercode(Long.parseLong(userid), order_code, pageAble);
					}
					
				}else if(startTime!=null){
						if(endTime!=null){
							return disRecordRepository.findDistributionRecordByUserId(Long.parseLong(userid), startTime, endTime, pageAble);
						}else{
							return disRecordRepository.findDistributionRecordByUserIdstart(Long.parseLong(userid), startTime, pageAble);
						}
				}else if(endTime!=null){
					return disRecordRepository.findDistributionRecordByUserIdend(Long.parseLong(userid), startTime, pageAble);
					
				}else{
					return disRecordRepository.findDistributionRecordByUserid(Long.parseLong(userid), pageAble);
				}
				
			}else if(order_code!=null&&!order_code.equals("")){
				return disRecordRepository.findDistributionRecordByOrdercode(order_code, pageAble);
			}else if(startTime!=null){
				if(endTime!=null ){
					return disRecordRepository.findDistributionRecordBystartend(startTime, endTime, pageAble);
				}
				
			}else{
				
				return disRecordRepository.findAllDistributionRecord( pageAble);	
				
			}
			
		}else{
			String[] ordercodes=this.getpaymentOrdercodes(brandid, "1", "1");
			boolean retn=false;
			if(userid!=null&&!userid.equals("")){
				if(order_code!=null&&!order_code.equals("")){
					if(ordercodes==null){
						return null;
					}
					for(String ordercode:ordercodes ){
						if(order_code.equals(ordercode)){
							retn=true;
							break;
						}
					}
					if(retn){
						if(startTime!=null){
							if(endTime!=null){
								return disRecordRepository.findDistributionRecordByUseridOrdercodestartend(Long.parseLong(userid), order_code, startTime, endTime, pageAble);
								
							}else{
								return disRecordRepository.findDistributionRecordByUseridOrdercodestart(Long.parseLong(userid), order_code, startTime, pageAble);
							}
							
						}else if(endTime!=null){
							
							return disRecordRepository.findDistributionRecordByUseridOrdercodeend(Long.parseLong(userid), order_code, endTime, pageAble);
						}else{
							
							return disRecordRepository.findDistributionRecordByUseridOrdercode(Long.parseLong(userid), order_code, pageAble);
						}
					}else{
						return null;
					}
					
					
				}else if(startTime!=null){
						if(endTime!=null){
							return disRecordRepository.findDistributionRecordByUserId(ordercodes,Long.parseLong(userid), startTime, endTime, pageAble);
						}else{
							return disRecordRepository.findDistributionRecordByUserIdstart(ordercodes,Long.parseLong(userid), startTime, pageAble);
						}
				}else if(endTime!=null){
					return disRecordRepository.findDistributionRecordByUserIdend(ordercodes,Long.parseLong(userid), startTime, pageAble);
					
				}else{
					return disRecordRepository.findDistributionRecordByUserid(ordercodes,Long.parseLong(userid), pageAble);
				}
				
			}else if(order_code!=null&&!order_code.equals("")){
				if(ordercodes==null){
					return null;
				}
				for(String ordercode:ordercodes ){
					if(order_code.equals(ordercode)){
						retn=true;
						break;
					}
				}
				if(retn){
					return disRecordRepository.findDistributionRecordByOrdercode(order_code, pageAble);
				}else{
					return null;
				}
				
			}else if(startTime!=null){
				if(endTime!=null ){
					return disRecordRepository.findDistributionRecordBystartend(ordercodes,startTime, endTime, pageAble);
				}
				
			}else{
				
				return disRecordRepository.findAllDistributionRecord(ordercodes, pageAble);	
				
			}
			
		}
		return disRecordRepository.findAllDistributionRecord( pageAble);
	}
	/***
	 * 获取brandid数据
	 * paymentOrderRepository
	 * */
	public String[]  getpaymentOrdercodes(long brandid , String type ,String status ){
		String[] str1 = type.split(",");
		String[] str2 = status.split(",");
		return paymentOrderRepository.findAllPaymentOrderordercodesbyBrandid(brandid, str1,str2);
	}

	//获取当前phone的数据库记录(oriphone为需要查询的手机号)
	@Override
	public List<ProfitRecord> queryProfitByOriPhone(String oriphone, String ordercode) {
		return profitRecordRepository.queryProfitByOriPhone(oriphone, ordercode);
	}

	//依据ordercode获取分润表中的订单信息
	@Override
	public List<ProfitRecord> queryProfitAmount(String ordercode) {
		return profitRecordRepository.queryProfitAmount(ordercode);
	}

	@Override
	public List<DistributionRecord> findAllDistributionByPhone(String acqphone,Date startTimeDate, Date endTimeDate, Pageable pageable) {
		Page<DistributionRecord> page = disRecordRepository.findAllDistributionByPhone(acqphone, startTimeDate, endTimeDate, pageable);
		return page.getContent();
	}

	@Override
	public Page<ProfitRecord> findProfitByUserid(String orderCode, Date startTimeDate, Date endTimeDate,
			Pageable pageable) {
		return profitRecordRepository.finByOrderCode(orderCode,startTimeDate,endTimeDate,pageable);
	}

	@Override
	public List<Object[]> findsumProfitRecordByAcqUserIds(long[] acqUserIds) {
		return profitRecordRepository.findsumProfitRecordByAcqUserIds(acqUserIds);
	}

	@Transactional
	@Override
	public void createNewProfitRecord(BigDecimal rebate, String preUserPhone, BigDecimal preRate, Long preUserId,
			BigDecimal amount, String ordercode, Long firstUserId, String firstUserPhone, BigDecimal firstRate,
			String type, BigDecimal scale, String description) {
		ProfitRecord profitRecord = new ProfitRecord();
		profitRecord.setAcqAmount(rebate);
		profitRecord.setAcqphone(preUserPhone);
		profitRecord.setAcqrate(preRate);
		profitRecord.setAcquserid(preUserId);
		profitRecord.setAmount(amount);
		profitRecord.setOrdercode(ordercode);
		profitRecord.setOriphone(firstUserPhone);
		profitRecord.setOriuserid(firstUserId);
		profitRecord.setOrirate(firstRate);
		profitRecord.setRemark(description);
		profitRecord.setScale(BigDecimal.ONE);
		profitRecord.setType(type);
		profitRecord.setCreateTime(new Date());
		profitRecordRepository.save(profitRecord);
		em.flush();
		em.clear();
	}

	@Override
	public Page<ProfitRecord> queryProfitAmountByPhone(String phone,String[] order,  String[] type, Date startTime, Date endTime,
			Pageable pageable) {
		// TODO Auto-generated method stub
		return profitRecordRepository.queryProfitAmountByPhone(phone, order,type, startTime, endTime, pageable);
	}

	@Override
	public Page<ProfitRecord> queryProfitAmountByGetPhone(String phone, String[] type, Date startTime, Date endTime,
			Pageable pageable) {
		// TODO Auto-generated method stub
		return profitRecordRepository.queryProfitAmountByGetPhone(phone, type, startTime, endTime, pageable);
	}

	

	
	@Override
	public Page<ProfitRecord> finByManyParams(String orderCode, String[] type, Date startTimeDate, Date endTimeDate,
			Pageable pageable) {
		// TODO Auto-generated method stub
		return profitRecordRepository.finByManyParams(orderCode, type, startTimeDate, endTimeDate, pageable);
	}

	@Override
	public Page<ProfitRecord> queryProfitAmountByDoublePhone(String phone, String getphone, String[] type,
			Date startTime, Date endTime, Pageable pageable) {
		// TODO Auto-generated method stub
		return profitRecordRepository.queryProfitAmountByDoublePhone(phone, getphone, type, startTime, endTime, pageable);
	}

	@Override
	public Page<ProfitRecord> queryProfitAmountByOderGetPhone(String getphone, String order, String[] type,
			Date startTime, Date endTime, Pageable pageable) {
		// TODO Auto-generated method stub
		return profitRecordRepository.queryProfitAmountByOderGetPhone(getphone, order, type, startTime, endTime, pageable);
	}

	@Override
	public Page<ProfitRecord> queryProfitAmountByOderPhone(String phone, String order, String[] type, Date startTime,
			Date endTime, Pageable pageable) {
		// TODO Auto-generated method stub
		return profitRecordRepository.queryProfitAmountByOderPhone(phone, order, type, startTime, endTime, pageable);
				
	}

	@Override
	public Page<ProfitRecord> queryByAllParams(String phone, String getphone, String order, String[] type,
			Date startTime, Date endTime, Pageable pageable) {
		// TODO Auto-generated method stub
		return profitRecordRepository.finByManyParams(order, type, startTime, endTime, pageable);
	}

	@Override
	public Object queryProfitAll(String phone, String[] type, Date startTime, Date endTime, Pageable pageable) {
		// TODO Auto-generated method stub
		return profitRecordRepository.queryProfitAll(phone, type, startTime, endTime, pageable);
	}

	@Override
	public BigDecimal queryProfitRecordSumAcqAmountByPhone(String phone, String startTime, String endTime) {
		
		
		StringBuffer sql = new StringBuffer("select sum(acq_amount) from t_profit_record where acq_phone=" + phone);
		
		if (startTime != null && !"".equals(startTime)) {
			sql.append(" and date_format(create_time,'%Y-%m-%d')>='" + startTime + "'");
		}
		if (endTime != null && !"".equals(endTime)) {
			sql.append(" and date_format(create_time,'%Y-%m-%d')<='" + endTime + "'");
		}
		
		Map<String, Object> profitSumAmount = jdbcTemplate.queryForMap(sql.toString());
		BigDecimal big = null;
		if (profitSumAmount.get("sum(acq_amount)") != null) {
			big = (BigDecimal) profitSumAmount.get("sum(acq_amount)");
		} else {
			double d = 0.00;
			big = big.valueOf(d);
		}
		
		return big;
	}



	
}
